<html>
<head>
<script>

  var currentState = "";

  function FindMusicBetaTab(callback) {
    chrome.windows.getAll({populate: true}, function(windows) {
        var pattern = 'https?\:\/\/music\.google\.com\/music\/listen.*';
        for (var window = 0; window < windows.length; window++) {
          for (var i = 0; i < windows[window].tabs.length; i++) {
            if (windows[window].tabs[i].url.match(pattern)) {
              callback(windows[window].tabs[i].id);
              return;
            }
          }
        }
        callback(null);
      });  
  }
  
    
  function sendCommand(command) {
    FindMusicBetaTab(function(tab_id) {
        if (tab_id) {
          if (command == "foreground") {
            chrome.tabs.update(tab_id, {selected: true});
          } else {
            chrome.tabs.executeScript(tab_id,
                {
                  code: "location.assign('javascript:SJBpost(\"" + command +
                        "\");void 0');",
                  allFrames: true
                });
          }
        } else {
          chrome.tabs.create({url: 'http://music.google.com/music/listen',
                              selected: true});
        }
        
      });
  }
  

  function UpdateIcon(state) {
    if (state == "Pause"){
       chrome.tabs.getAllInWindow(null, function(tabs){
          tabs.forEach(function(tab){
             localStorage["icon"] = "ic_media_pause.png";          
    	     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	     chrome.pageAction.show(tab.id);
  	  });
       });
    }
    else if (state == "Play"){
       chrome.tabs.getAllInWindow(null, function(tabs){
          tabs.forEach(function(tab){
             localStorage["icon"] = "ic_media_play.png";
    	     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	     chrome.pageAction.show(tab.id);
  	  });
       });    
    }
    else{
       chrome.tabs.getAllInWindow(null, function(tabs){
          tabs.forEach(function(tab){
             localStorage["icon"] = "ic_media_play.png";          
    	     chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
	     chrome.pageAction.show(tab.id);
  	  });
       });    
    }
  }



	function UpdateIconFromPageState(tabId, changeInfo, tab) {
	    FindMusicBetaTab(function(tab_id) {
	        if (tab_id){
	           chrome.tabs.sendRequest(tab_id, "getPlayState", UpdateIcon);	        
	        }
	        else{
	           chrome.tabs.getAllInWindow(null, function(tabs){
	              tabs.forEach(function(tab){
	                 localStorage["icon"] = "ic_media_play.png";          
	    	         chrome.pageAction.setIcon({"tabId": tab.id, "path":localStorage["icon"]});
		         chrome.pageAction.show(tab.id);
	  	      });
                    });  
	        }

	    });
	}

  UpdateIconFromPageState();

  // React to tabs notifications by updating our icon state if needed.
  chrome.tabs.onAttached.addListener(UpdateIconFromPageState);
  chrome.tabs.onDetached.addListener(UpdateIconFromPageState);
  chrome.tabs.onCreated.addListener(UpdateIconFromPageState);
  chrome.tabs.onRemoved.addListener(UpdateIconFromPageState);
  chrome.tabs.onUpdated.addListener(UpdateIconFromPageState);
 
 
  // Called when the user clicks on the browser action.
  chrome.pageAction.onClicked.addListener(function (tab) {
      sendCommand('playPause');
    });

  // Called when the Play/Pause state changes.
  chrome.extension.onRequest.addListener(function(message, sender, callback) {
      UpdateIconFromPageState();
      callback();
    });
    



</script>
</head>
</html>
